# 需要爬取的地址： http://www.tianqihoubao.com/lishi/shenzhen/month/202412.html
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import re
import pandas as pd
from random import randint

df = pd.DataFrame({
    'datetime': [],
    'day_weather': [],
    'night_weather': [],
    'temperature_low': [],
    'temperature_top': [],
    'day_wind_direction': [],  # 白天风向
    'night_wind_direction': [],  # 夜晚风向
    'day_wind_min_power': [],  # 白天最小风力
    'day_wind_max_power': [],  # 白天最大风力
    'night_wind_min_power': [],  # 夜晚最大风力
    'night_wind_max_power': [],  # 夜晚最大风力
})

options = webdriver.EdgeOptions()
driver = webdriver.Edge(options)  # 启用edge浏览器


def generate_weather_data_by_month(city, year, month):
    format_date = f"{year}{month:02d}"
    driver.get(f"http://www.tianqihoubao.com/lishi/{city}/month/{format_date}.html")  # 访问天气网站
    trs = driver.find_elements(By.CSS_SELECTOR, "#content table tr")
    for tr in trs[1:]:
        tds = tr.find_elements(By.CSS_SELECTOR, "td")
        date = re.findall(r"(\d+)年(\d+)月(\d+)日", tds[0].text)[0]
        weather = re.findall(r"(\S+) /(\S+)", tds[1].text)[0]
        temperature = re.findall(r"(-?\d+)℃ / (-?\d+)℃", tds[2].text)[0]
        winds = re.findall(r"(\S+) (\d+)-(\d+)级 /(\S+) (\d+)-(\d+)级", tds[3].text)[0]

        df.loc[len(df)] = [
            ''.join(date),
            weather[0],
            weather[1],
            int(temperature[0]),
            int(temperature[1]),
            winds[0],
            winds[3],
            int(winds[1]),
            int(winds[2]),
            int(winds[4]),
            int(winds[5])
        ]


def save(path="../data/weather.csv"):
    df.to_csv(path, index=False, encoding='utf-8')


def generate_weather_data_by_year(city, year):
    for month in range(1, 13):
        secs = randint(1, 4)
        time.sleep(secs)
        generate_weather_data_by_month(city, year, month)
    save()


if __name__ == '__main__':
    # generate_weather_data_by_year('shenzhen', '2024')
    generate_weather_data_by_month('shenzhen', '2024',8)
    save()
